<template>
  <div class="app-container">
    <el-row>
      <el-button type="primary" plain icon="el-icon-plus" @click="addInsuranceType" style="margin-bottom: 20px">添加vip</el-button>
    </el-row>
    <el-table ref="table" :data="tableData">
      <el-table-column align="center" prop="vipName" label="vip名称"> </el-table-column>
      <el-table-column align="center" prop="vipLevel" label="vip等级"> </el-table-column>
      <el-table-column align="center" prop="discountRate" label="优惠利率（单位%）">
      </el-table-column>
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="editVip(scope.row)">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="delVip(scope.row.vipId)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog :title="vipTitle" :visible.sync="dialogVisible" width="40%" v-loading="loading">
      <el-form :model="addVipForm" ref="addVipForm" :rules="rules" label-width="140px">
        <el-form-item label="vip名称" style="width: 100%" prop="vipName">
          <el-input v-model="addVipForm.vipName" placeholder="vip名称"></el-input>
        </el-form-item>
        <el-form-item label="vip等级" style="width: 100%" prop="vipLevel">
          <el-input v-model="addVipForm.vipLevel" placeholder="vip等级"></el-input>
        </el-form-item>
        <el-form-item label="优惠利率(单位%)" style="width: 100%" prop="discountRate">
          <el-input v-model="addVipForm.discountRate" placeholder="优惠利率（单位%）"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cance">取 消</el-button>
        <el-button type="primary" @click="determine">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
  <script>
export default {
  data() {
    return {
      loading: false,
      vipTitle: "添加VIP",
      editId: "",
      addVipForm: {
        vipName: "",
        vipLevel: "",
        discountRate: "",
        sort: "",
      },
      dialogVisible: false,
      // form: {
      //     user: '',
      //     region: ''
      // },
      tableData: [],
      tableHeight: 0,
      rules: {
        vipName: [
          { required: true, message: "请输入vip名称", trigger: "blur" },
        ],
        vipLevel: [
          { required: true, message: "请输入vip等级", trigger: "blur" },
        ],
        discountRate: [
          { required: true, message: "请输入优惠费率", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    determine() {
      this.$refs.addVipForm.validate((valid) => {
        if (valid) {
          if (this.loading) {
            return;
          }
          this.loading = true;
          const url = this.editId ? "customer/vip" : "customer/vip";
          const func = this.editId ? "$put" : "$post";
          const params = this.editId
            ? { ...this.addVipForm, vipId: this.editId }
            : this.addVipForm;
          this[func](url, params)
            .then((res) => {
              if (res && res.code * 1 === 200) {
                this.$message({
                  type: "success",
                  message: this.editId ? "修改成功" : "新增成功",
                });
                this.$refs.addVipForm.resetFields();
                this.dialogVisible = false;
                this.getTableData();
              }
            })
            .finally(() => {
              this.loading = false;
            });
        }
      });
    },
    delVip(vipId) {
      this.$confirm("是否确认删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$delete(`/customer/vip/${vipId}`).then((res) => {
            if (res && res.code * 1 === 200) {
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              this.getTableData();
            }
          });
        })
        .catch(() => {
          //
        });
    },
    cance() {
      this.$refs.addVipForm.resetFields();
      this.dialogVisible = false;
    },
    addInsuranceType() {
      this.$nextTick(() => {
        this.vipTitle = "添加Vip";
        this.dialogVisible = true;
        this.addVipForm.vipName = "";
        this.addVipForm.vipLevel = "";
        this.addVipForm.discountRate = "";
        this.editId = "";
      });
    },
    editVip(row) {
      this.$nextTick(() => {
        this.vipTitle = "修改Vip";
        this.dialogVisible = true;
        this.addVipForm.vipName = row.vipName;
        this.addVipForm.vipLevel = row.vipLevel;
        this.addVipForm.vipLevel = row.vipLevel;
        this.addVipForm.discountRate = row.discountRate;
        this.editId = row.vipId;
      });
    },
    getTableData() {
      this.$get("/customer/vip/list").then((res) => {
        this.tableData = res.rows;
      });
    },
  },
  mounted() {
    this.getTableData();
  },
};
</script>
  <style scoped lang="scss">
.form-inline ::v-deep {
  width: 100%;

  .el-form-item__label {
    // padding: 0;
  }
}
</style>
  